package com.soft.javase.day09.collections;

import java.util.*;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/29
 */
public class CardGame {
    // 定义花色和数字
    private static final String[] COLORS = {"♠", "♥", "♣", "♦"};
    private static final String[] NUMBERS = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};

    public static void main(String[] args) {

        List<Card> cards = initCards();
        System.out.println("初始牌组: " + cards);
        System.out.println("牌组大小: " + cards.size());

        Collections.shuffle(cards);
        System.out.println("\n洗牌后: " + cards);

        List<Card> player1 = new ArrayList<>();
        List<Card> player2 = new ArrayList<>();
        List<Card> player3 = new ArrayList<>();
        List<Card> hiddenCards = new ArrayList<>();

        for (int i = 0; i < cards.size(); i++) {
            Card card = cards.get(i);
            if (i < 51) { // 前51张牌发给玩家
                if (i % 3 == 0) {
                    player1.add(card);
                } else if (i % 3 == 1) {
                    player2.add(card);
                } else {
                    player3.add(card);
                }
            } else { // 最后3张作为底牌
                hiddenCards.add(card);
            }
        }

        sortCards(player1);
        sortCards(player2);
        sortCards(player3);
        sortCards(hiddenCards);

        System.out.println("\n玩家1的牌: " + player1);
        System.out.println("玩家2的牌: " + player2);
        System.out.println("玩家3的牌: " + player3);
        System.out.println("底牌: " + hiddenCards+"\n");

        checkBigJoker(player1, player2, player3, hiddenCards);
    }

    // 初始化牌组
    private static List<Card> initCards() {
        List<Card> cards = new ArrayList<>();
        int index = 0;

        cards.add(new Card("", "大王", index++));
        cards.add(new Card("", "小王", index++));

        for (String number : NUMBERS) {
            for (String color : COLORS) {
                cards.add(new Card(color, number, index++));
            }
        }

        return cards;
    }

    // 排序牌
    private static void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o2.getIndex()-o1.getIndex();
            }
        });
    }

    // 检查谁有大王可以拿底牌
    private static void checkBigJoker(List<Card> player1, List<Card> player2, List<Card> player3, List<Card> hiddenCards) {
        boolean p1Has = hasBigJoker(player1);
        boolean p2Has = hasBigJoker(player2);
        boolean p3Has = hasBigJoker(player3);

        if (p1Has) {
            System.out.println("玩家1有大王，可以拿底牌");
            player1.addAll(hiddenCards);
            sortCards(player1);
            System.out.println("玩家1拿到底牌后的牌: " + player1);
        }
        if (p2Has) {
            System.out.println("玩家2有大王，可以拿底牌");
            player2.addAll(hiddenCards);
            sortCards(player2);
            System.out.println("玩家2拿到底牌后的牌: " + player2);
        }
        if (p3Has) {
            System.out.println("玩家3有大王，可以拿底牌");
            player3.addAll(hiddenCards);
            sortCards(player3);
            System.out.println("玩家3拿到底牌后的牌: " + player3);
        }
        if (!p1Has && !p2Has && !p3Has) {
            Random random = new Random();
            int luckyPlayer = random.nextInt(3) + 1;
            switch (luckyPlayer) {
                case 1:
                    player1.addAll(hiddenCards);
                    sortCards(player1);
                    System.out.println("\n无人有大王，随机选择玩家1获得底牌");
                    System.out.println("玩家1拿到底牌后的牌: " + player1);
                    break;
                case 2:
                    player2.addAll(hiddenCards);
                    sortCards(player2);
                    System.out.println("\n无人有大王，随机选择玩家2获得底牌");
                    System.out.println("玩家2拿到底牌后的牌: " + player2);
                    break;
                case 3:
                    player3.addAll(hiddenCards);
                    sortCards(player3);
                    System.out.println("\n无人有大王，随机选择玩家3获得底牌");
                    System.out.println("玩家3拿到底牌后的牌: " + player3);
                    break;
            }
        }
    }

    // 检查是否有大王
    private static boolean hasBigJoker(List<Card> player) {
        for (Card card : player) {
            if ("大王".equals(card.getNumber())) {
                return true;
            }
        }
        return false;
    }
}
